<template>
  <div id="app">
    <el-container>
      <el-header>
        <div class="logo-box">
          <img src="../assets/img/heima.png" alt="" />
          <span>电商后台管理系统</span>
        </div>
        <el-button type="info" @click="logout()">退出</el-button>
      </el-header>
      <el-container>
        <el-aside :width="menushow ? `65px` : `200px`">
          <div
            :width="menushow ? `65px` : `200px`"
            class="toggle_bar"
            @click="menushow = !menushow"
          >
            |||
          </div>
          <el-menu
            background-color="#333744"
            text-color="#fff"
            active-text-color="#409EFF"
            :collapse-transition="false"
            :unique-opened="true"
            :collapse="menushow"
            :width="menushow ? `65px` : `200px`"
            router
            :default-active="$route.path"
          >
            <!--:collapse-transition="false"折叠时候禁用动画
            :unique-opened="true"每次只展开一个项目 -->
            <!-- outer 是否使用 vue-router 的模式，
            启用该模式会在激活导航时以 index 作为 path 进行路由跳转-->
            <el-submenu
              v-for="item in menuList"
              :key="item.id"
              :index="item.id + ''"
              :style="{ width: menushow ? `65px` : `200px` }"
            >
              <template slot="title">
                <i :class="`iconfont icon-` + icons[`ic` + item.id]"></i>
                <span>{{ item.authName }}</span>
              </template>
              <el-menu-item
                :index="'/' + item2.path"
                v-for="item2 in item.children"
                :key="item2.id"
                ><i class="el-icon-menu"></i>
                <span>{{ item2.authName }}</span>
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>
        <el-main><router-view></router-view></el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
export default {
  created() {
    this.getMenuList();
  },
  methods: {
    async getMenuList() {
      var { data: dt } = await this.$http.get("/menus");
      // console.log(dt);
      if (dt.meta.status !== 200) {
        return this.$message.error(dt.msg);
      }
      this.menuList = dt.data;
    },
    logout() {
      this.$confirm("确定要退出登录系统吗?", "退出", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.$message({
            type: "success",
            message: "退出成功!"
          });
          window.sessionStorage.removeItem("token");
          this.$router.push("/login");
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消退出"
          });
        });
    }
  },
  data() {
    return {
      menushow: false, //true折叠，false不折叠
      menuList: [],
      icons: {
        ic125: "31haoyou",
        ic103: "attention",
        ic101: "tianmaopaidui",
        ic102: "31dingdan",
        ic145: "paixing"
      }
    };
  }
};
</script>

<style lang="less" scoped>
.el-container {
  height: 100%;
  .el-header {
    background-color: #373d41;
    padding: 0;
    display: flex;
    align-items: center; //控制垂直居中
    justify-content: space-between; //左边和右边的button分开左右两边
    padding-right: 20px;
    .logo-box {
      display: flex;
      font-size: 22px;
      color: #fff;
      align-items: center; //控制垂直居中
      user-select: none; //不让被选中
      img {
        width: 50px;
        height: 50px;
        margin-right: 10px;
      }
    }
  }
  .el-aside {
    background-color: #333744;
    .toggle_bar {
      height: 25px;
      color: #fff;
      text-align: center;
      line-height: 25px;
      letter-spacing: 0.1em;
      background-color: #4a5064;
      font-size: 12px;
      user-select: none;
      cursor: pointer;
    }
  }
  .el-main {
    background-color: #eaedf1;
  }
}
</style>
